package com.nova.edc.service.api.impl;

import com.nova.edc.dao.api.ReportMenuDao;
import com.nova.edc.model.api.ReportMenu;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.api.ReportMenuService;
import com.nova.frame.dao.buzz.query.Order;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Administrator on 2016/5/30 0030.
 */
public class ReportMenuServiceImpl implements ReportMenuService {

    @Autowired
    private ReportMenuDao novaReportMenuDao;

    @Override
    public List<ReportMenu> findByParent(Integer parentID) {
        Query<ReportMenu> query = novaReportMenuDao.query()
                .add(Restrictions.eq("r_leftmenus.ParentID", parentID))
                .addOrder(Order.asc("r_leftmenus.Ord"));
        return query.list();
    }

    @Override
    public List<ReportMenu> findByLevel(Integer level) {
        Query<ReportMenu> query = novaReportMenuDao.query()
                .add(Restrictions.eq("r_leftmenus.Level", level))
                .addOrder(Order.asc("r_leftmenus.Ord"));
        return query.list();
    }

    @Override
    public List<ReportMenu> findAllMenuList() {
        List<ReportMenu> menuList = findByLevel(ModelConstant.MENU_FIRST);
        for (ReportMenu menu : menuList) {
            List<ReportMenu> secondMenu = findByParent(menu.getId());
            for (ReportMenu second : secondMenu) {
                List<ReportMenu> lastMenu = findByParent(second.getId());
                second.setNextmenus(lastMenu);
            }
            menu.setNextmenus(secondMenu);
        }
        return menuList;
    }
}
